//
//  ReportFackViewController.swift
//  AESOLAR_IOS
//
//  Created by Mr.Wang on 2019/8/29.
//  Copyright © 2019 Mr.Wang. All rights reserved.
//

import UIKit
import RxSwift
import RxDataSources

class ReportFackViewController: BaseUIViewController {
    
    let contentScrollView = UIScrollView()
    let contentView = UIView()
    
    let codeText = FakeTextfield()
    let compactText = FakeTextfield()
    let channelText = FakeTextfield()
    let supplierText = FakeTextfield()
    let numText = FakeTextfield()
    let bottomLabel = UILabel()
    
    let psrCheckId: String
    var editModel: FakeModel
    let barCode: String
    
    init(psrCheckId: String, editModel: FakeModel? = nil, barCode: String = "") {
        self.psrCheckId = psrCheckId
        self.editModel = editModel ?? FakeModel()
        self.editModel.psrCheckId = psrCheckId
        self.barCode = barCode
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupView()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }
    
    func setupView() {
        self.view.backgroundColor = #colorLiteral(red: 0.9724436402, green: 0.972609818, blue: 0.9724331498, alpha: 1)
        self.navigationItem.title = "报假信息".localized
        
        contentScrollView.config
            .keyboardDismissMode(.onDrag)
            .alwaysBounceVertical(true)
            .backgroundColor(#colorLiteral(red: 0.9724436402, green: 0.972609818, blue: 0.9724331498, alpha: 1))
            .addTo(view)
            .makeConstraints { (make) in
                make.edges.equalToSuperview()
        }
        
        contentView.config
            .backgroundColor(.clear)
            .frame(x: 0, y: 0, width: SCREEN_WIDTH, height: 0)
            .addTo(contentScrollView)
        
        let topLabel = UILabel().config
            .text("请填写报假信息，我们将尽快提供反馈".localized)
            .textColor(#colorLiteral(red: 0.6, green: 0.6, blue: 0.6, alpha: 1))
            .numberOfLines(0)
            .font(.systemFont(ofSize: 12, weight: .medium))
            .addTo(contentView)
            .makeConstraints { (make) in
                make.left.equalTo(15)
                make.top.equalTo(20)
                make.right.lessThanOrEqualTo(-15)
        }.view
        
        codeText.textField.isEnabled = false
        codeText.isUserInteractionEnabled = false
        codeText.placeHolder = "序列号".localized
        codeText.text = editModel.productNumber.count > 0 ? editModel.productNumber : barCode
        contentView.addSubview(codeText)
        codeText.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.top.equalTo(topLabel.snp.bottom).offset(20)
            make.height.equalTo(50)
        }
        
        compactText.optional = true
        compactText.text = editModel.contractNo
        compactText.placeHolder = "合同号".localized
        contentView.addSubview(compactText)
        compactText.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.top.equalTo(codeText.snp.bottom).offset(10)
            make.height.equalTo(50)
        }
        
        channelText.optional = true
        channelText.text = editModel.purchaseChannel
        channelText.placeHolder = "采购渠道".localized
        contentView.addSubview(channelText)
        channelText.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.top.equalTo(compactText.snp.bottom)
            make.height.equalTo(50)
        }
        
        supplierText.optional = true
        supplierText.text = editModel.supplier
        supplierText.placeHolder = "供应商".localized
        contentView.addSubview(supplierText)
        supplierText.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.top.equalTo(channelText.snp.bottom)
            make.height.equalTo(50)
        }
        
        numText.optional = true
        numText.text = editModel.purchaseNum
        numText.placeHolder = "采购数量".localized
        contentView.addSubview(numText)
        numText.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.top.equalTo(supplierText.snp.bottom)
            make.height.equalTo(50)
        }
        
        let button = UIButton().config
            .backgroundImage(UIImage.imageWithColor(APPConfig.baseColor))
            .title("下一步".localized)
            .cornerRadius(5)
            .clipsToBounds(true)
            .titleColor(.white)
            .addTarget(self, action: #selector(nextStep))
            .addTo(contentView)
            .makeConstraints { (make) in
                make.centerX.equalToSuperview()
                make.left.equalTo(40)
                make.height.equalTo(50)
                make.top.equalTo(numText.snp.bottom).offset(65)
        }.view
        
        let text = NSMutableAttributedString.init(string: "跳过后可前往 反馈 入口补充报假信息， 以便我们为您提供更好的帮助。".localized)
        text.setTextColor(APPConfig.baseColor, text: "反馈".localized)
        bottomLabel.textAlignment = .center
        bottomLabel.textColor = #colorLiteral(red: 0.6, green: 0.6, blue: 0.6, alpha: 1)
        bottomLabel.numberOfLines = 0
        bottomLabel.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        bottomLabel.attributedText = text
        self.contentView.addSubview(bottomLabel)
        bottomLabel.snp.makeConstraints { (make) in
            make.top.equalTo(button.snp.bottom).offset(20)
            make.centerX.equalToSuperview()
            make.left.equalTo(75)
        }
        
        let hasFilled = Observable.combineLatest(compactText.rx.text.orEmpty.asObservable(),
                                                 channelText.rx.text.orEmpty.asObservable(),
                                                 supplierText.rx.text.orEmpty.asObservable(),
                                                 numText.rx.text.orEmpty.asObservable())
            .map { (compact, channel, supplier, num) -> Bool in
                return compact.count > 0 ||
                    channel.count > 0 ||
                    supplier.count > 0 ||
                    num.count > 0
        }.share(replay: 1)
        
        hasFilled.bind(to: bottomLabel.rx.isHidden)
            .disposed(by: rx.disposeBag)
        
        hasFilled.map{ $0 ? "下一步".localized : "跳过".localized }
            .bind(to: button.rx.title(for: .normal))
            .disposed(by: rx.disposeBag)
    }
    
    @objc func nextStep() {
        let code = codeText.text
        let compact = compactText.text
        let channel = channelText.text
        let supplier = supplierText.text
        let num = numText.text
        
        editModel.productNumber =  code
        editModel.purchaseChannel = channel
        editModel.contractNo = compact
        editModel.supplier = supplier
        editModel.purchaseNum = num
        
        let vc = ReportFack3ViewController.init(editModel: editModel)
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let maxY = contentView.subviews.map{ $0.frame.maxY }.max() ?? 0
        contentView.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: maxY + UIApplication.shared.bottomSafeDistance + 20)
        contentScrollView.contentSize = contentView.size
    }
    
    override func navigationBarStyle() -> NavigationBarStyle {
        return .baseColor
    }
    
}

